
package example

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/example"
    exampleReq "github.com/flipped-aurora/gin-vue-admin/server/model/example/request"
    "gorm.io/gorm"
)

type WorkInformationService struct {}
// CreateWorkInformation 创建workInformation表记录
// Author [yourname](https://github.com/yourname)
func (workInformationService *WorkInformationService) CreateWorkInformation(ctx context.Context, workInformation *example.WorkInformation) (err error) {
	err = global.GVA_DB.Create(workInformation).Error
	return err
}

// DeleteWorkInformation 删除workInformation表记录
// Author [yourname](https://github.com/yourname)
func (workInformationService *WorkInformationService)DeleteWorkInformation(ctx context.Context, ID string,userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
	    if err := tx.Model(&example.WorkInformation{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
              return err
        }
        if err = tx.Delete(&example.WorkInformation{},"id = ?",ID).Error; err != nil {
              return err
        }
        return nil
	})
	return err
}

// DeleteWorkInformationByIds 批量删除workInformation表记录
// Author [yourname](https://github.com/yourname)
func (workInformationService *WorkInformationService)DeleteWorkInformationByIds(ctx context.Context, IDs []string,deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
	    if err := tx.Model(&example.WorkInformation{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
            return err
        }
        if err := tx.Where("id in ?", IDs).Delete(&example.WorkInformation{}).Error; err != nil {
            return err
        }
        return nil
    })
	return err
}

// UpdateWorkInformation 更新workInformation表记录
// Author [yourname](https://github.com/yourname)
func (workInformationService *WorkInformationService)UpdateWorkInformation(ctx context.Context, workInformation example.WorkInformation) (err error) {
	err = global.GVA_DB.Model(&example.WorkInformation{}).Where("id = ?",workInformation.ID).Updates(&workInformation).Error
	return err
}

// GetWorkInformation 根据ID获取workInformation表记录
// Author [yourname](https://github.com/yourname)
func (workInformationService *WorkInformationService)GetWorkInformation(ctx context.Context, ID string) (workInformation example.WorkInformation, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&workInformation).Error
	return
}
// GetWorkInformationInfoList 分页获取workInformation表记录
// Author [yourname](https://github.com/yourname)
func (workInformationService *WorkInformationService)GetWorkInformationInfoList(ctx context.Context, info exampleReq.WorkInformationSearch) (list []example.WorkInformation, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&example.WorkInformation{})
    var workInformations []example.WorkInformation
    // 如果有条件搜索 下方会自动创建搜索语句
    if len(info.CreatedAtRange) == 2 {
     db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
    }
    
    if info.VideoType != nil && *info.VideoType != "" {
        db = db.Where("video_type = ?", *info.VideoType)
    }
    if info.User != nil && *info.User != "" {
        db = db.Where("user = ?", *info.User)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&workInformations).Error
	return  workInformations, total, err
}
func (workInformationService *WorkInformationService)GetWorkInformationPublic(ctx context.Context) {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
